package com.example.uniappspringbootstartweb.controller;

import com.example.uniappspringbootstartweb.entity.Content;
import com.example.uniappspringbootstartweb.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@CrossOrigin(origins = "http://localhost:8080")
@RestController
@RequestMapping("/api/items")
public class ContentController {


    @Autowired
    private ContentService contentService;
    @GetMapping ("/all")
    public  ResponseEntity<List<Content>> getAllContents() {
        List<Content> allContents = contentService.getAllContents();

        List<Content> top10Contents = allContents.stream()
                .limit(10)
                .collect(Collectors.toList());
        return ResponseEntity.ok(top10Contents);
    }

    @GetMapping("/a/{id}") // 获取单个内容
    public ResponseEntity<?> getContentByIdAndTab(@PathVariable("id") Long id,
                                                  @RequestParam(value = "tab", required = false) String tab) {
        Optional<Content> content = contentService.getContentByIdAndTab(id, tab);
        return content.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());
    }
    @GetMapping // 获取内容列表
    public ResponseEntity<List<Content>> getContents(@RequestParam(required = false) String tab,
                                                     @RequestParam(required = false) String title) {
        if (tab != null && !tab.isEmpty()) {
            List<Content> contentsByTab = contentService.getContentByTab(tab);
            System.out.println("contentsByTab: " + contentsByTab.toString());
            return ResponseEntity.ok(contentsByTab);
        } else if (title != null && !title.isEmpty()) {
            List<Content> searchedContents = contentService.searchContents(title);
            System.out.println("searchedContents: " + searchedContents.toString());
            return ResponseEntity.ok(searchedContents);
        } else {
            System.out.println("allContents: " + contentService.getAllContents().toString());
            List<Content> allContents = contentService.getAllContents();
            return ResponseEntity.ok(allContents);
        }
    }

    @PostMapping
    public Content createContent(@RequestBody Content content) {

        return contentService.saveContent(content);
    }

    @PutMapping("/{id}")
    public Content updateContent(@PathVariable Long id, @RequestBody Content contentDetails) {
        Content content = contentService.getContentById(id);
        if (content != null) {
            content.setTitle(contentDetails.getTitle());
            content.setDescription(contentDetails.getDescription());
            content.setImage(contentDetails.getImage());
            content.setVideoUrl(contentDetails.getVideoUrl());
            content.setTab(contentDetails.getTab());
            return contentService.saveContent(content);
        }
        return null;
    }

    @GetMapping("/{id}")
    public ResponseEntity<Content> getContentById(@PathVariable Long id) {
        Content content = contentService.getContentById(id);
        if (content == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(content);
    }
}



